import task

class TaskList(list):
    
    def __str__(self):
        ret = str()
        for i, task in enumerate(self):
            ret += "\n%d - %s" % (i+1, str(task))
        return ret
    
    def doCommand(self):
        print self
        print "command ([n]ew, [d]elete, [t]ick, [u]ntick, [q]uit): "
        cmd = raw_input()
        if self.parseCommand(cmd):
            self.doCommand()
    
    def parseCommand(self, cmd):
        try:
            if cmd == 'q':
                return False
            elif cmd == 'n':
                new = task.Task()
                self.append(new)
            elif cmd == 't':
                id = self.readID()
                self[id-1].tick()
            elif cmd == 'd':
                id = self.readID()
                del(self[id-1])
            elif cmd == 'u':
                id = self.readID()
                self[id-1].untick()
        except Exception:
            pass
        return True

            
    def readID(self):
        if len(self) < 1:
            raise Exception('List is empty');
        id = -1;
        while not self.validateID(id):
            try:
                id = int(raw_input("task num [1-%d]: " % len(self)))
            except Exception:
                continue
        return id

    def validateID(self, id):
        return ((0 < id) and (id <= len(self)))

